package com.jumper.study.common.captcha.impl;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.jumper.study.common.captcha.ICaptchaStore;

/**
 * Redis保存验证码
 */
@Service
public class RedisCaptchaStore implements ICaptchaStore {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void save(String code, Integer expireTime) {
        redisTemplate.opsForValue().set(code, code);
        redisTemplate.expire(code, expireTime, TimeUnit.SECONDS);
    }

    @Override
    public boolean verify(String code) {
        String result = redisTemplate.opsForValue().get(code);
        return result != null;
    }

    @Override
    public boolean remove(String code) {
        return redisTemplate.delete(code);
    }
    
}
